package down.xiaoheihe.work.Controller;

import down.xiaoheihe.work.Entity.*;
import down.xiaoheihe.work.Mapper.TOrderMapper;
import down.xiaoheihe.work.Mapper.TProductMapper;
import down.xiaoheihe.work.Mapper.TimagesMapper;
import down.xiaoheihe.work.Mapper.UserMapper;
import down.xiaoheihe.work.Response.Gender;
import down.xiaoheihe.work.utils.ElasticUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@MapperScan("down.xiaoheihe.work.Mapper")
@CrossOrigin
public class ProductsController {
    @Autowired
    TimagesMapper imagesMapper;
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    TProductMapper tProductMapper;
    @Autowired
    UserMapper tUserMapper;
    @Autowired
    ElasticUtils utils;
    @GetMapping("/goods")
    public Map 查询指定商品 (@RequestParam Integer id) {
        TProduct tProducts = tProductMapper.selectByPrimaryKey(id);
        int productId = tProducts.getId();
        TimagesExample timagesExample = new TimagesExample();
        timagesExample.createCriteria().andProductIdEqualTo(productId);
        List<Timages> timages = imagesMapper.selectByExample(timagesExample);
        List<String> urls = new ArrayList<>();
        for (Timages image: timages) {
            urls.add(image.getUrl());
        }
        Map map = Map.of("title",tProducts.getTitle(),"id",tProducts.getId(),"description",tProducts.getDescription(),"price",tProducts.getPrice(),"guarentee", tProducts.getGuarentee(),"imageUrls",urls,"createrId",tProducts.getCreaterId(),"status",tProducts.getStatus());
        return map;
    }
    @GetMapping("/mygoods")
    public Map getMyGoods(){
        Subject subject = SecurityUtils.getSubject();
        Integer id = (Integer) subject.getPrincipal();
        TProductExample example = new TProductExample();
        example.createCriteria().andCreaterIdEqualTo(id);
        List<TProduct> tProducts = tProductMapper.selectByExample(example);
        List<Map> myProducts = new ArrayList<>();
        for (TProduct product: tProducts) {
            TimagesExample timagesExample = new TimagesExample();
            timagesExample.createCriteria().andProductIdEqualTo(product.getId());
            List<Timages> timages = imagesMapper.selectByExample(timagesExample);
            List<String> urls = new ArrayList<>();
            for (Timages image: timages) {
                urls.add(image.getUrl());
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id",product.getId());
            map.put("title",product.getTitle());
            map.put("description",product.getDescription());
            map.put("price",product.getPrice());
            map.put("guarentee", product.getGuarentee());
            map.put("createrId",product.getCreaterId());
            map.put("imageUrls",urls);
            myProducts.add(map);
        }
        return Map.of("myProducts",myProducts);
    }
    @PatchMapping("/update_pro")
    public TProduct updatePro(@RequestBody TProduct tProduct) throws IOException {
        tProductMapper.updateByPrimaryKeySelective(tProduct);
        utils.updateProducts(tProduct);
        return tProduct;
    }

    @GetMapping("/selectProducts")
    public List<Integer> 表格 () {
        List<Integer> result = new ArrayList<>();
        TProductExample a = new TProductExample();
        a.createCriteria().andPriceBetween((long) 0,(long)4.999);
        List<TProduct> tProducts = tProductMapper.selectByExample(a);
        result.add(tProducts.size());
        TProductExample b = new TProductExample();
        b.createCriteria().andPriceBetween((long) 5,(long)14.999);
        result.add(tProductMapper.selectByExample(b).size());
        TProductExample c = new TProductExample();
        c.createCriteria().andPriceBetween((long) 15,(long)49.999);
        result.add(tProductMapper.selectByExample(c).size());
        TProductExample d = new TProductExample();
        d.createCriteria().andPriceBetween((long) 50,(long)999);
            result.add(tProductMapper.selectByExample(d).size());
        return result;
    }
    @GetMapping("/selectGender")
    public Gender 性别 () {
        Gender gender = new Gender();
        int man = 0;
        int woman = 0;
        List<User> users = tUserMapper.selectByExample(new UserExample());
        for (User user : users) {
            if (user.getGender()==1) {
                man++;
            }else {
                woman++;
            }
        }
        gender.setMan(man);
        gender.setGirl(woman);
        return gender;
    }
    @GetMapping("/selectFlex")
    public Map<String,List<Integer>> flex () {
        Map<String,List<Integer>> result = new HashMap<>();
        List<Integer> q = new ArrayList<>();
        List<Integer> w = new ArrayList<>();
        List<Integer> e = new ArrayList<>();
        List<Integer> r = new ArrayList<>();
        Date today = new Date(System.currentTimeMillis());
        Date last1Day = new Date(today.getTime()-24*60*60*1000);
        Date last2Day = new Date(today.getTime()-(24*60*60*1000)*2);
        Date last3Day = new Date(today.getTime()-(24*60*60*1000)*3);
        Date last4Day = new Date(today.getTime()-(24*60*60*1000)*4);
        Date last5Day = new Date(today.getTime()-(24*60*60*1000)*5);
        Date last6Day = new Date(today.getTime()-(24*60*60*1000)*6);
        Date last7Day = new Date(today.getTime()-(24*60*60*1000)*7);
        TOrderExample exampleq1 = new TOrderExample();
        exampleq1.createCriteria().andCreateTimeBetween(last7Day,last6Day).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq1).size());
        TOrderExample exampleq2 = new TOrderExample();
        exampleq2.createCriteria().andCreateTimeBetween(last6Day,last5Day).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq2).size());
        TOrderExample exampleq3 = new TOrderExample();
        exampleq3.createCriteria().andCreateTimeBetween(last5Day,last4Day).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq3).size());
        TOrderExample exampleq4 = new TOrderExample();
        exampleq4.createCriteria().andCreateTimeBetween(last4Day,last3Day).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq4).size());
        TOrderExample exampleq5 = new TOrderExample();
        exampleq5.createCriteria().andCreateTimeBetween(last3Day,last2Day).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq5).size());
        TOrderExample exampleq6 = new TOrderExample();
        exampleq6.createCriteria().andCreateTimeBetween(last2Day,last1Day).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq6).size());
        TOrderExample exampleq7 = new TOrderExample();
        exampleq7.createCriteria().andCreateTimeBetween(last1Day,today).andTotalPriceBetween((long)0,(long)1999.999);
        q.add(tOrderMapper.selectByExample(exampleq7).size());

        TOrderExample examplew1 = new TOrderExample();
        examplew1.createCriteria().andCreateTimeBetween(last7Day,last6Day).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew1).size());
        TOrderExample examplew2 = new TOrderExample();
        examplew2.createCriteria().andCreateTimeBetween(last6Day,last5Day).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew2).size());
        TOrderExample examplew3 = new TOrderExample();
        examplew3.createCriteria().andCreateTimeBetween(last5Day,last4Day).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew3).size());
        TOrderExample examplew4 = new TOrderExample();
        examplew4.createCriteria().andCreateTimeBetween(last4Day,last3Day).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew4).size());
        TOrderExample examplew5 = new TOrderExample();
        examplew5.createCriteria().andCreateTimeBetween(last3Day,last2Day).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew5).size());
        TOrderExample examplew6 = new TOrderExample();
        examplew6.createCriteria().andCreateTimeBetween(last2Day,last1Day).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew6).size());
        TOrderExample examplew7 = new TOrderExample();
        examplew7.createCriteria().andCreateTimeBetween(last1Day,today).andTotalPriceBetween((long)2000,(long)3999.999);
        w.add(tOrderMapper.selectByExample(examplew7).size());

        TOrderExample examplee1 = new TOrderExample();
        examplee1.createCriteria().andCreateTimeBetween(last7Day,last6Day).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee1).size());
        TOrderExample examplee2 = new TOrderExample();
        examplee2.createCriteria().andCreateTimeBetween(last6Day,last5Day).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee2).size());
        TOrderExample examplee3 = new TOrderExample();
        examplee3.createCriteria().andCreateTimeBetween(last5Day,last4Day).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee3).size());
        TOrderExample examplee4 = new TOrderExample();
        examplee4.createCriteria().andCreateTimeBetween(last4Day,last3Day).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee4).size());
        TOrderExample examplee5 = new TOrderExample();
        examplee5.createCriteria().andCreateTimeBetween(last3Day,last2Day).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee5).size());
        TOrderExample examplee6 = new TOrderExample();
        examplee6.createCriteria().andCreateTimeBetween(last2Day,last1Day).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee6).size());
        TOrderExample examplee7 = new TOrderExample();
        examplee7.createCriteria().andCreateTimeBetween(last1Day,today).andTotalPriceBetween((long)4000,(long)6999.999);
        e.add(tOrderMapper.selectByExample(examplee7).size());

        TOrderExample exampler1 = new TOrderExample();
        exampler1.createCriteria().andCreateTimeBetween(last7Day,last6Day).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler1).size());
        TOrderExample exampler2 = new TOrderExample();
        exampler2.createCriteria().andCreateTimeBetween(last6Day,last5Day).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler2).size());
        TOrderExample exampler3 = new TOrderExample();
        exampler3.createCriteria().andCreateTimeBetween(last5Day,last4Day).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler3).size());
        TOrderExample exampler4 = new TOrderExample();
        exampler4.createCriteria().andCreateTimeBetween(last4Day,last3Day).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler4).size());
        TOrderExample exampler5 = new TOrderExample();
        exampler5.createCriteria().andCreateTimeBetween(last3Day,last2Day).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler5).size());
        TOrderExample exampler6 = new TOrderExample();
        exampler6.createCriteria().andCreateTimeBetween(last2Day,last1Day).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler6).size());
        TOrderExample exampler7 = new TOrderExample();
        exampler7.createCriteria().andCreateTimeBetween(last1Day,today).andTotalPriceBetween((long)7000,(long)20000);
        r.add(tOrderMapper.selectByExample(exampler7).size());
        result.put("q",q);
        result.put("w",w);
        result.put("e",e);
        result.put("r",r);
        return result;
    }
}
